<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入产品订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产时间" prop="productionTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.productionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择生产时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="生产厂商" prop="manufacturer">
        <el-input
          v-model="queryParams.manufacturer"
          placeholder="请输入生产厂商"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createdBy">
        <el-input
          v-model="queryParams.createdBy"
          placeholder="请输入创建人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createdTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.createdTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['admin:product:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['admin:product:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['admin:product:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:product:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品id" align="center" prop="id" />
      <el-table-column label="产品所属订单号" align="center" prop="orderNumber" />
      <el-table-column label="库存序号" align="center" prop="sn" />
      <el-table-column label="产品型号" align="center" prop="model" />
      <el-table-column label="订单数量" align="center" prop="orderQuantity" />
      <el-table-column label="生产时间" align="center" prop="productionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.productionTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产厂商" align="center" prop="manufacturer" />
      <el-table-column label="机器颜色" align="center" prop="machineColor" />
      <el-table-column label="面板需求" align="center" prop="panelDemand" />
      <el-table-column label="具体方案" align="center" prop="concreteProposal" />
      <el-table-column
        label="主板info"
        align="center" prop="mainBoard.id" width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>版本: {{ scope.row.mainBoard.version }}</p>
            <p>内存: {{ scope.row.mainBoard.memory }}</p>
            <p>芯片: {{ scope.row.mainBoard.emmc }}</p>
            <p>wifi模块: {{ scope.row.mainBoard.wifi }}</p>
            <p>pmv: {{ scope.row.mainBoard.pmv }}</p>
            <p>gSensor: {{ scope.row.mainBoard.gSensor }}</p>
            <p>功效IL: {{ scope.row.mainBoard.effectIl }}</p>
            <p>音效IL: {{ scope.row.mainBoard.soundIl }}</p>
            <p>是否带MIC: {{ scope.row.mainBoard.isMic == '2' ? '是' : '否' }}</p>
            <p>是否有纽扣电池: {{ scope.row.mainBoard.isButtonCell == '2' ? '是' : '否' }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">型号: {{ scope.row.mainBoard.model }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="显示器" align="center" prop="lcd" />
      <el-table-column label="网络域名代码" align="center" prop="tp" />
      <el-table-column label="电源" align="center" prop="powerSource" />
      <el-table-column label="说明书" align="center" prop="specification" />
      <el-table-column label="彩盒" align="center" prop="graphicCarton" />
      <el-table-column label="安卓版本" align="center" prop="androidVersion" />
      <el-table-column label="内核版本" align="center" prop="kernelVersion" />
      <el-table-column label="apk版本" align="center" prop="apkVersion" />
      <el-table-column label="固件版本" align="center" prop="firmwareVersion" />
      <el-table-column label="wifi Mac地址" align="center" prop="wifiMacAddress" />
      <el-table-column label="Bt  Mac地址" align="center" prop="btMacAddress" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" align="center" prop="updatedBy" />
      <el-table-column label="更新时间" align="center" prop="updatedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['admin:product:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['admin:product:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品所属订单号" prop="orderNumber">
          <el-input v-model="form.orderNumber" placeholder="请输入产品所属订单号" />
        </el-form-item>
        <el-form-item label="库存序号" prop="sn">
          <el-input v-model="form.sn" placeholder="请输入库存序号" />
        </el-form-item>
        <el-form-item label="产品型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入产品型号" />
        </el-form-item>
        <el-form-item label="订单数量" prop="orderQuantity">
          <el-input v-model="form.orderQuantity" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="生产时间" prop="productionTime">
          <el-date-picker clearable size="small"
            v-model="form.productionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择生产时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="生产厂商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂商" />
        </el-form-item>
        <el-form-item label="机器颜色" prop="machineColor">
          <el-input v-model="form.machineColor" placeholder="请输入机器颜色" />
        </el-form-item>
        <el-form-item label="面板需求" prop="panelDemand">
          <el-input v-model="form.panelDemand" placeholder="请输入面板需求" />
        </el-form-item>
        <el-form-item label="具体方案" prop="concreteProposal">
          <el-input v-model="form.concreteProposal" placeholder="请输入具体方案" />
        </el-form-item>
        <el-form-item label="显示器" prop="lcd">
          <el-input v-model="form.lcd" placeholder="请输入显示器" />
        </el-form-item>
        <el-form-item label="网络域名代码" prop="tp">
          <el-input v-model="form.tp" placeholder="请输入网络域名代码" />
        </el-form-item>
        <el-form-item label="电源" prop="powerSource">
          <el-input v-model="form.powerSource" placeholder="请输入电源" />
        </el-form-item>
        <el-form-item label="说明书" prop="specification">
          <el-input v-model="form.specification" placeholder="请输入说明书" />
        </el-form-item>
        <el-form-item label="彩盒" prop="graphicCarton">
          <el-input v-model="form.graphicCarton" placeholder="请输入彩盒" />
        </el-form-item>
        <el-form-item label="安卓版本" prop="androidVersion">
          <el-input v-model="form.androidVersion" placeholder="请输入安卓版本" />
        </el-form-item>
        <el-form-item label="内核版本" prop="kernelVersion">
          <el-input v-model="form.kernelVersion" placeholder="请输入内核版本" />
        </el-form-item>
        <el-form-item label="apk版本" prop="apkVersion">
          <el-input v-model="form.apkVersion" placeholder="请输入apk版本" />
        </el-form-item>
        <el-form-item label="固件版本" prop="firmwareVersion">
          <el-input v-model="form.firmwareVersion" placeholder="请输入固件版本" />
        </el-form-item>
        <el-form-item label="wifi Mac地址" prop="wifiMacAddress">
          <el-input v-model="form.wifiMacAddress" placeholder="请输入wifi Mac地址" />
        </el-form-item>
        <el-form-item label="Bt  Mac地址" prop="btMacAddress">
          <el-input v-model="form.btMacAddress" placeholder="请输入Bt  Mac地址" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdTime">
          <el-date-picker clearable size="small"
            v-model="form.createdTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新人" prop="updatedBy">
          <el-input v-model="form.updatedBy" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedTime">
          <el-date-picker clearable size="small"
            v-model="form.updatedTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记位" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记位" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-divider content-position="center">主板信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddMainBoard">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteMainBoard">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="mainBoardList" :row-class-name="rowMainBoardIndex" @selection-change="handleMainBoardSelectionChange" ref="mainBoard">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="型号" prop="model">
            <template slot-scope="scope">
              <el-input v-model="scope.row.model" placeholder="请输入型号" />
            </template>
          </el-table-column>
          <el-table-column label="版本" prop="version">
            <template slot-scope="scope">
              <el-input v-model="scope.row.version" placeholder="请输入版本" />
            </template>
          </el-table-column>
          <el-table-column label="内存" prop="memory">
            <template slot-scope="scope">
              <el-input v-model="scope.row.memory" placeholder="请输入内存" />
            </template>
          </el-table-column>
          <el-table-column label="芯片" prop="emmc">
            <template slot-scope="scope">
              <el-input v-model="scope.row.emmc" placeholder="请输入芯片" />
            </template>
          </el-table-column>
          <el-table-column label="wifi模块" prop="wifi">
            <template slot-scope="scope">
              <el-input v-model="scope.row.wifi" placeholder="请输入wifi模块" />
            </template>
          </el-table-column>
          <el-table-column label="待补充-" prop="pmv">
            <template slot-scope="scope">
              <el-input v-model="scope.row.pmv" placeholder="请输入待补充-" />
            </template>
          </el-table-column>
          <el-table-column label="待补充-" prop="gSensor">
            <template slot-scope="scope">
              <el-input v-model="scope.row.gSensor" placeholder="请输入待补充-" />
            </template>
          </el-table-column>
          <el-table-column label="功效IL" prop="effectIl">
            <template slot-scope="scope">
              <el-input v-model="scope.row.effectIl" placeholder="请输入功效IL" />
            </template>
          </el-table-column>
          <el-table-column label="音效IL" prop="soundIl">
            <template slot-scope="scope">
              <el-input v-model="scope.row.soundIl" placeholder="请输入音效IL" />
            </template>
          </el-table-column>
          <el-table-column label="是否带MIC" prop="isMic">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isMic" placeholder="请输入是否带MIC" />
            </template>
          </el-table-column>
          <el-table-column label="是否有纽扣电池" prop="isButtonCell">
            <template slot-scope="scope">
              <el-input v-model="scope.row.isButtonCell" placeholder="请输入是否有纽扣电池" />
            </template>
          </el-table-column>
          <el-table-column label="创建人" prop="createdBy">
            <template slot-scope="scope">
              <el-input v-model="scope.row.createdBy" placeholder="请输入创建人" />
            </template>
          </el-table-column>
          <el-table-column label="创建时间" prop="createdTime">
            <template slot-scope="scope">
              <el-input v-model="scope.row.createdTime" placeholder="请输入创建时间" />
            </template>
          </el-table-column>
          <el-table-column label="更新人" prop="updatedBy">
            <template slot-scope="scope">
              <el-input v-model="scope.row.updatedBy" placeholder="请输入更新人" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updatedTime">
            <template slot-scope="scope">
              <el-input v-model="scope.row.updatedTime" placeholder="请输入更新时间" />
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入备注" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct } from "@/api/admin/product";

export default {
  name: "Product",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMainBoard: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品表格数据
      productList: [],
      // 主板表格数据
      mainBoardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        productionTime: null,
        manufacturer: null,
        createdBy: null,
        createdTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        sn: null,
        model: null,
        orderQuantity: null,
        productionTime: null,
        manufacturer: null,
        machineColor: null,
        panelDemand: null,
        concreteProposal: null,
        lcd: null,
        tp: null,
        powerSource: null,
        specification: null,
        graphicCarton: null,
        androidVersion: null,
        kernelVersion: null,
        apkVersion: null,
        firmwareVersion: null,
        wifiMacAddress: null,
        btMacAddress: null,
        createdBy: null,
        createdTime: null,
        updatedBy: null,
        updatedTime: null,
        delFlag: null,
        remark: null
      };
      this.mainBoardList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProduct(id).then(response => {
        this.form = response.data;
        // this.form.mainBoardId = this.form.mainBoardId.split(",");
        this.mainBoardList[0] = response.data.mainBoard;
        this.open = true;
        this.title = "修改产品";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // this.form.mainBoardId = this.form.mainBoardId.join(",");
          this.form.mainBoard = this.mainBoardList[0];
          if (this.form.id != null) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除产品编号为"' + ids + '"的数据项？').then(function() {
        return delProduct(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 主板序号 */
    rowMainBoardIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 主板添加按钮操作 */
    handleAddMainBoard() {
      let obj = {};
      obj.model = "";
      obj.version = "";
      obj.memory = "";
      obj.emmc = "";
      obj.wifi = "";
      obj.pmv = "";
      obj.gSensor = "";
      obj.effectIl = "";
      obj.soundIl = "";
      obj.isMic = "";
      obj.isButtonCell = "";
      obj.createdBy = "";
      obj.createdTime = "";
      obj.updatedBy = "";
      obj.updatedTime = "";
      obj.remark = "";
      this.mainBoardList.push(obj);
    },
    /** 主板删除按钮操作 */
    handleDeleteMainBoard() {
      if (this.checkedMainBoard.length == 0) {
        this.$modal.msgError("请先选择要删除的主板数据");
      } else {
        const mainBoardList = this.mainBoardList;
        const checkedMainBoard = this.checkedMainBoard;
        this.mainBoardList = mainBoardList.filter(function(item) {
          return checkedMainBoard.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMainBoardSelectionChange(selection) {
      this.checkedMainBoard = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('admin/product/export', {
        ...this.queryParams
      }, `product_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
